package org.jmetrix.et;

import org.jmetrix.api.Aggregator;

/**
 * @author Konstantin Kuzmin
 * @version $Revision$
 * @since 25.09.11
 */
public class ExecutionTimeAggregator implements TimeAggregator, Aggregator<ExecutionTime> {
    private Long min;
    private Long max;
    private Long total;
    private long count = 0;
    private Double average;

    @Override
    public synchronized void aggregate(ExecutionTime executionTime) {
        final Long value = executionTime.getValue();
        if (min == null || min.compareTo(value) > 0) {
            min = value;
        }
        if (max == null || max.compareTo(value) < 0) {
            max = value;
        }
        if (total == null) {
            total = value;
        } else {
            total += value;
        }
        count++;
        average = total.doubleValue() / count;
    }

    @Override
    public void doReset() {
        reset();
    }

    @Override
    public synchronized void reset() {
        min = null;
        max = null;
        total = null;
        average = null;
        count = 0;
    }

    public Long getMin() {
        return min;
    }

    public Long getMax() {
        return max;
    }

    public Long getTotal() {
        return total;
    }

    public long getCount() {
        return count;
    }

    public Double getAverage() {
        return average;
    }

    @Override
    public String toString() {
        return "ExecutionTime [ " +
                "average=" + average +
                ", count=" + count +
                ", total=" + total +
                ", max=" + max +
                ", min=" + min +
                " ]";
    }
}

